<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Common\Entity\BO;

use Jhub1_Common\Entity\BO\Extensible;
use Jhub1_Common\Entity\BO\AbstractBO;
use Jhub1_Common\Entity\Agents;
use Jhub1_Common\Entity\BO\EAVBO;
use Jhub1_Common\Entity\Users;
use Jhub1_Common\Model\SettingsObjects\AgentLocalConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\AgentRemoteConfigurationObject;
use Jhub1_Common\Model\Key\UniqueKey;
use Jhub1_Common\Model\SettingsObjects\DateTimeObject;

class AgentsBO extends AbstractBO
{
    private $agentPO = null;
    private $serviceLocator;

    public function __construct($serviceLocator)
    {
        $this->getEM($serviceLocator);
        $this->serviceLocator = $serviceLocator;
    }

    public function getAgentPO()
    {
        return $this->agentPO;
    }

    public function addAgent($userPO, $agentName, $agentDescription)
    {
        $kg = new UniqueKey($this);
        $username = $kg->getAgentUUID();
        $this->em->getConnection()->beginTransaction();
        try {
            $agent = new Agents($userPO);
            $agent->setId($kg->getUUID());
            $agent->setAgentName($agentName);
            $agent->setAgentDesc($agentDescription);
            $agent->setLocalConfiguration(new AgentLocalConfigurationObject());
            $agent->setRemoteConfiguration(new AgentRemoteConfigurationObject());
            $agent->setOrder(0);
            $agent->setUserame($username); // generate user name and make sure it is unique among the whole agents
            $this->em->persist($agent);
            $this->em->getConnection()->commit();
        } catch (Exception $e) {
            $this->em->getConnection()->rollback();
            $this->em->close();
            throw $e;
        }
        $this->em->flush();
        $this->agentPO = $agent;
/*
       $agenteav =  new AgentsEAV($agent);
       $agenteav->setName("testVal1");
       $agenteav->setText("Test text value");
        $this->em->persist($agenteav);
        $this->em->flush();
*/

/*        $agenteav =  new EAVBO($agent, $this->em);
        $agenteav->putString("daadsada", "sadaddasd as d ad as dsa d sad as d");
        $agenteav->putString("da33423da", "s23423424 234 23 4 234 23 3 3 adaddasd as d ad as dsa d sad as d");
        $agenteav->removeString("daadsada");
        $agenteav->putString("da33423da", "s2XXXXXXXXXXXXXXXXx");
        $agenteav->putText("da33423da", "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY");

        echo $agenteav->getString("da33423da");
        echo $agenteav->getText("da33423da");

        $agenteav->removeString("da33423da");*/
        /*
        $agenteav->setName("testVal1");
        $agenteav->setText("Test text value");
        $this->em->persist($agenteav);
        $this->em->flush();*/

        return $agent;

        /*
        > INSERT INTO `ofUser` VALUES ('myfirstuser',NULL,'fe8ea5be7f276fb03e6cadb5665743ac3e6ddfd44a5d635c','Marek','marek.kaszewski@gmail.com','001381531343277','001381531343277');
        > INSERT INTO `ofID` VALUES (18,1),(19,1),(23,1),(26,2),(25,4);
        > INSERT INTO `ofSecurityAuditLog` VALUES (1,'admin',1381529915527,'enabled db profiling','jhub1',NULL),(2,'admin',1381531343289,'created new user myfirstuser','jhub1','name = Marek, email = marek.kaszewski@gmail.com, admin = false'),(3,'admin',1381531432964,'created new user myseconduser','jhub1','name = mIsha, email = marek.k.a.szewski@gmail.com, admin = false');
        > INSERT INTO `ofGroupUser` VALUES ('mojagrupa','myfirstuser',0);
        */

    }

    public function getAgentsList(Users $userPO)
    {
        $retArr = array();
        $agents = $this->getAgents($userPO);
        foreach($agents as $value) {
            $retArr[] = $value->getUserame();
        }
        return $retArr;
    }

    public function getAgents(Users $userPO)
    {
        return $userPO->getAgents();
    }

    public function getAgentByUserId($userId)
    {
        $agent = $this->em->getRepository('\Jhub1_Common\Entity\Agents')->findOneBy(array('username' => $userId));
        if ($agent instanceof  \Jhub1_Common\Entity\Agents) {
            return $agent;
        }
        return null;
    }

    public function deleteAgent($name)
    {

    }

    public function getAgentApp($agentName)
    {

    }

    public function isPKUnique($primaryKey)
    {
        if(null == $this->em->getRepository('\Jhub1_Common\Entity\Agents')->find($primaryKey)) {
            return true;
        }
        return false;
    }

    public function isAgentIdUnique($agentId)
    {
        if (null == $this->getAgentByUserId($agentId)) {
            return true;
        }
        return false;
    }

    public function getServiceLocator()
    {
        return $this->serviceLocator;
    }

    public function getObjectByUUID($uuid)
    {
        return $this->em->getRepository('\Jhub1_Common\Entity\Agents')->find($uuid);
    }
}